package com.jerkin.datastructure.linkedlist.single;

/**
 * @version V1.0
 * @ClassName: SingleLinkedList
 * @Description:单向链表得结构实现
 * @author: huhongzhi
 * @date: 2019/12/19 21:22
 */
public class SingleLinkedList {

    private SingleNode head;

    public void add(String value){
        SingleNode singleNode = new SingleNode(value);
        //判断是否没有头节点
        if(hasHead()){
            head = singleNode;
            return;
        }
        SingleNode temp = head;
        //循环找到尾节点
        while (!isTail(temp)){
            temp = temp.getNext();
        }
        //变更尾节点
        temp.setNext(singleNode);
    }

    public Boolean deleteByIndex(int index){
        if(index<0||index>getLength()){
            return false;
        }
        return true;

    }

    public Integer getLength(){
        Integer length = 0;
        if(hasHead()){
            return 0;
        }
        SingleNode temp = head;
        while(!isTail(temp)){
            length++;
            temp = temp.getNext();
        }
        return length;

    }

    /**
     * 判断头节点
     * @return
     */
    private Boolean hasHead(){
        return head==null;
    }

    /**
     * 判断尾节点
     * @param node
     * @return
     */
    private Boolean isTail(SingleNode node){
        return node.getNext()==null;
    }
    
}
